package com.fishingwithme.android.ui.screens.my.settings

import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.fishingwithme.android.R
import com.fishingwithme.android.ui.theme.AppDimensions
import com.fishingwithme.android.ui.viewmodels.AgreementPolicyViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AgreementPolicyScreen(
    navController: NavController,
    type: String,
    viewModel: AgreementPolicyViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsState()

    LaunchedEffect(type) {
        viewModel.loadContent(type.trim())
    }

    Scaffold { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
        ) {
            if (uiState.isLoading) {
                CircularProgressIndicator(
                    modifier = Modifier.padding(AppDimensions.SPACING_16)
                )
            } else if (uiState.error != null) {
                Text(
                    text = uiState.error!!,
                    color = MaterialTheme.colorScheme.error,
                    modifier = Modifier.padding(AppDimensions.SPACING_16)
                )
            } else {
                AndroidView(
                    factory = { context ->
                        WebView(context).apply {
                            layoutParams = ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT
                            )
                            webViewClient = WebViewClient()
                        }
                    },
                    update = { webView ->
                        webView.loadDataWithBaseURL(
                            null,
                            uiState.content,
                            "text/html",
                            "UTF-8",
                            null
                        )
                    },
                    modifier = Modifier.weight(1f)
                )
            }

            // Add the Agree button at the bottom
            Button(
                onClick = {
                    // Pass back the agreement type when navigating back
                    navController.previousBackStackEntry?.savedStateHandle?.set("agreedType", type)
                    navController.popBackStack()
                },
                modifier = Modifier
                    .padding(AppDimensions.SPACING_16)
                    .align(Alignment.CenterHorizontally)
            ) {
                Text(text = stringResource(R.string.view_and_agree))
            }
        }
    }
}